from enum import Enum
from pydantic.dataclasses import dataclass
from pydantic import Field
from typing import List


@dataclass
class PriceScope:
    min: float
    max: float

class Platform(Enum):
    pdd = "拼多多"
    tb = "淘宝"

    @classmethod
    def valueOf(cls, value):
        for item in cls:
            if item.value == value:
                return item
        return None


class Status(Enum):
    Created = 1
    Moving = 2
    Finish = 3
    Error = 4

    @classmethod
    def valueOf(cls, value):
        return {
            "新建": cls.Created,
            "搬家中": cls.Moving,
            "搬家成功": cls.Finish,
            "搬家失败": cls.Error,
        }.get(value, None)
    

@dataclass
class Goods:
    id: int = 0,
    sku: str = 0,
    url: str = ""
    title: str = ""
    img: str = ""
    price: PriceScope = None
    number: int = 0
    platform: Platform = None
    createTime: str = ""
    status: Status = None
    movingNumber: int = 0

    @classmethod
    def parse(cls, tr):
        # parse
        info_lines = tr("text:标题").text.split("\n")
        price_range = list(map(float, info_lines[2].split("：")[1].split("-")))
        

        # TODO: 构建对象
        obj = cls()
        obj.id = int(info_lines[0].split("：")[1])
        obj.sku = tr("@data-id").attr("data-id")
        obj.title = info_lines[1].split("：")[1]
        obj.price = PriceScope(price_range[0], price_range[1])
        obj.number = int(info_lines[3].split("：")[1])
        obj.img = tr("tag:img").attrs["src"]
        obj.platform = Platform.valueOf(tr("tag:td", index=4).text.strip())
        obj.movingNumber = int(tr("tag:td", index=6).text)
        obj.status = Status.valueOf(tr("tag:td", index=7).text.strip())
        obj.url = f"https://mobile.yangkeduo.com/goods.html?goods_id={obj.id}"
        obj.createTime = tr("tag:td", index=8).text.strip()
        return obj

@dataclass
class SKUInfo(object):
    id: str = ""
    title: str = ""
    imgList: List[str] = Field(default_factory=list)